#include "audio.h"
#include <QAudioInput>
#include <QAudioOutput>
#include <QAudioFormat>
#include <QTcpSocket>
#include <QIODevice>
#include <QDebug>

AudioService::AudioService(QObject *parent)
    : QObject(parent)
    , audioInput(nullptr)
    , audioOutput(nullptr)
    , inputDevice(nullptr)
    , outputDevice(nullptr)
    , networkSocket(new QTcpSocket(this))
    , protocol(nullptr)
    , enabled(false)
    , capturing(false)
{
    // 设置默认音频格式
    format.setSampleRate(44100);
    format.setChannelCount(2);
    format.setSampleFormat(QAudioFormat::Int16);
    
    initializeAudio();
    
    // 连接网络套接字信号
    connect(networkSocket, &QTcpSocket::readyRead,
            this, &AudioService::onNetworkDataReceived);
}

AudioService::~AudioService()
{
    stopCapture();
    cleanupAudio();
}

void AudioService::setAudioFormat(const QAudioFormat &format)
{
    if (this->format != format) {
        this->format = format;
        
        // 重新初始化音频设备
        cleanupAudio();
        if (enabled) {
            initializeAudio();
        }
    }
}

QAudioFormat AudioService::audioFormat() const
{
    return format;
}

void AudioService::setEnabled(bool enabled)
{
    if (this->enabled != enabled) {
        this->enabled = enabled;
        
        if (enabled) {
            initializeAudio();
        } else {
            stopCapture();
            cleanupAudio();
        }
    }
}

bool AudioService::isEnabled() const
{
    return enabled;
}

void AudioService::setProtocol(QObject *protocol)
{
    this->protocol = protocol;
}

void AudioService::startCapture()
{
    if (!enabled || !audioInput || capturing)
        return;
        
    if (inputDevice) {
        inputDevice = audioInput->start();
        connect(inputDevice, &QIODevice::readyRead,
                this, &AudioService::onAudioDataReady);
        capturing = true;
    }
}

void AudioService::stopCapture()
{
    if (!capturing)
        return;
        
    if (audioInput) {
        audioInput->stop();
        capturing = false;
    }
    
    if (inputDevice) {
        disconnect(inputDevice, &QIODevice::readyRead,
                   this, &AudioService::onAudioDataReady);
        inputDevice = nullptr;
    }
}

void AudioService::playAudioData(const QByteArray &data)
{
    if (!enabled || !audioOutput || data.isEmpty())
        return;
        
    if (!outputDevice) {
        outputDevice = audioOutput->start();
    }
    
    if (outputDevice) {
        outputDevice->write(data);
    }
}

void AudioService::onAudioDataReady()
{
    if (!inputDevice || !protocol)
        return;
        
    // 读取音频数据
    QByteArray data = inputDevice->readAll();
    if (!data.isEmpty()) {
        // 在实际实现中，这里会将音频数据发送到远程
        // 通过协议处理器发送
        qDebug() << "捕获到音频数据:" << data.size() << "字节";
    }
}

void AudioService::onAudioStateChanged()
{
    // 处理音频设备状态变化
}

void AudioService::onNetworkDataReceived()
{
    // 处理从网络接收到的音频数据
    QByteArray data = networkSocket->readAll();
    if (!data.isEmpty()) {
        playAudioData(data);
    }
}

void AudioService::initializeAudio()
{
    if (!enabled)
        return;
        
    // 初始化音频输入设备
    audioInput = new QAudioInput(format, this);
    connect(audioInput, &QAudioInput::stateChanged,
            this, &AudioService::onAudioStateChanged);
    
    // 初始化音频输出设备
    audioOutput = new QAudioOutput(format, this);
    connect(audioOutput, &QAudioOutput::stateChanged,
            this, &AudioService::onAudioStateChanged);
}

void AudioService::cleanupAudio()
{
    stopCapture();
    
    if (audioInput) {
        delete audioInput;
        audioInput = nullptr;
    }
    
    if (audioOutput) {
        audioOutput->stop();
        delete audioOutput;
        audioOutput = nullptr;
    }
    
    inputDevice = nullptr;
    outputDevice = nullptr;
}